<?php

class ControladorPublicacao extends ControladorFather {

    public $publicacao;

    public function __construct() {
        $this->publicacao = new Publicacao(host, dbname, port, user, pass);
    }

    public function index() {
        
    }

    public function listC($param = 0) {
        if ($param != "") {
            if ($param[0] == 0) {
                $categorias = new Categoria(host, dbname, port, user, pass);
                $cat = $categorias->find(array('order' => array('name')));
                $result = $this->publicacao->find(array('order' => array('nome')));
                include_once("View/Publicacao/listC.php");
            } else {
                $categorias = new Categoria(host, dbname, port, user, pass);
                $cat = $categorias->find(array('order' => array('name')));
                $result = $this->publicacao->find(array('order' => array('nome'), 'condition' => array('categoria' => $param[0])));
                include_once("View/Publicacao/listC.php");
            }
        }
    }

    public function edit($param) {
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $date = $_POST['anoPub'] . '-' . $_POST['mesPub'] . '-' . $_POST['diaPub'];
            $data = array(
                'nome' => $_POST['nomePub'],
                'edicao' => $_POST['edicaoPub'],
                'orientador' => $_POST['orientadorPub'],
                'assunto' => $_POST['assuntoPub'],
                'autor' => $_POST['autorPub'],
                'categoria' => $_POST['categoriaPub'],
                'datapublicacao' => $date
            );
            if ($this->publicacao->save($param[0], $data))
                $result = 1;
            else
                $result = 0;
        }
        $categorias = new Categoria(host, dbname, port, user, pass);
        $autores = new Autor(host, dbname, port, user, pass);
        $cat = $categorias->find(array('order' => array('name')));
        $autores = $autores->find(array('order' => array('name')));
        $date = $this->publicacao->findById($param[0]);
        include_once("View/Publicacao/edit.php");
    }

    public function delete($param) {
        $this->publicacao->delete($param[0]);
        header("Location: index.php?c=Publicacao&a=listC&p=0");
    }

    public function add() {

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {

            $uploaddir = 'File/';
            
            if(!($_FILES['userfile']['type'] == 'application/pdf')){
                $result = 0;
                include_once("View/Publicacao/add.php");
                return 0;
            }
            $name = mcrypt_create_iv(20);
            $name.= $_FILES['userfile']['name'];
            $name = sha1($name).'.pdf';
            

            if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir . $name)) {
                
                return 0;
            } 

            $date = $_POST['anoPub'] . '-' . $_POST['mesPub'] . '-' . $_POST['diaPub'];
            $data = array(
                'nome' => $_POST['nomePub'],
                'edicao' => $_POST['edicaoPub'],
                'orientador' => $_POST['orientadorPub'],
                'assunto' => $_POST['assuntoPub'],
                'autor' => $_POST['autorPub'],
                'categoria' => $_POST['categoriaPub'],
                'datapublicacao' => $date,
                'file' => $name
            );
            if ($this->publicacao->save(0, $data))
                $result = 1;
            else
                $result = 0;
        }
        $categorias = new Categoria(host, dbname, port, user, pass);
        $autores = new Autor(host, dbname, port, user, pass);
        $cat = $categorias->find(array('order' => array('name')));
        $autores = $autores->find(array('order' => array('name')));
        include_once("View/Publicacao/add.php");
    }

}

?>
